package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

import java.util.List;

/**
 * @description:
 * @author: Hupengpeng
 * @time: 2021/7/3 15:02
 */
public class FansCheck {
    private String open_id;
    private String follower_open_id;
    private String access_token;
    private String response;

    private FansCheck() {
    }

    public static FansCheck New() {
        return new FansCheck();
    }

    public FansCheck execute() {
        String url = StrUtil.format("{}{}?open_id={}&access_token={}&follower_open_id={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_FANS_CHECK,
                getOpen_id(),
                getAccess_token(),
                getFollower_open_id()
        );
        String rs = HttpUtil.get(url);
        response = rs;
        return this;
    }

    public FansCheckResponse convert() {
        try {
            JSONObject obj = JSONUtil.parseObj(response);
            JSONObject data = obj.getJSONObject("data");
            if (obj.getJSONObject("extra").getInt("error_code") == 0) {
                return JSONUtil.toBean(data, FansCheckResponse.class);
            }
        } catch (Exception e) {
            throw new DouYinException(StrUtil.format("{}:{}", e.getMessage(), response));
        }
        throw new DouYinException(response);
    }

    public String getOpen_id() {
        return open_id;
    }

    public FansCheck setOpen_id(String open_id) {
        this.open_id = open_id;
        return this;
    }

    public String getFollower_open_id() {
        return follower_open_id;
    }

    public FansCheck setFollower_open_id(String follower_open_id) {
        this.follower_open_id = follower_open_id;
        return this;
    }

    public String getAccess_token() {
        return access_token;
    }

    public FansCheck setAccess_token(String access_token) {
        this.access_token = access_token;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public FansCheck setResponse(String response) {
        this.response = response;
        return this;
    }

    public class FansCheckResponse {
        private long follow_time;
        private boolean is_follower;
        private int error_code;
        private String description;

        public long getFollow_time() {
            return follow_time;
        }

        public FansCheckResponse setFollow_time(long follow_time) {
            this.follow_time = follow_time;
            return this;
        }

        public boolean isIs_follower() {
            return is_follower;
        }

        public FansCheckResponse setIs_follower(boolean is_follower) {
            this.is_follower = is_follower;
            return this;
        }

        public int getError_code() {
            return error_code;
        }

        public FansCheckResponse setError_code(int error_code) {
            this.error_code = error_code;
            return this;
        }

        public String getDescription() {
            return description;
        }

        public FansCheckResponse setDescription(String description) {
            this.description = description;
            return this;
        }
    }
}
